package cn.tedu.egnotice2.controller;

import cn.tedu.egnotice2.base.response.JsonResult;
import cn.tedu.egnotice2.base.response.StatusCode;
import cn.tedu.egnotice2.mapper.NoticeMapper;
import cn.tedu.egnotice2.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice2.pojo.dto.NoticeAddQuery;
import cn.tedu.egnotice2.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice2.pojo.entiy.Notice;
import cn.tedu.egnotice2.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice2.pojo.vo.NoticeListVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.Jar;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("add")
    public JsonResult add(@RequestBody NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam = " + noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int i=noticeMapper.insertNotice(notice);
        log.debug("i = " + i);
        if(i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("list")
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery = " + noticeAddQuery);
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> list=
                noticeMapper.selectNotice(title,type);
        return JsonResult.ok(list);

    }
    @GetMapping("detail")
    public JsonResult detail(Integer id){
        log.debug("id = " + id);
        NoticeDetailInfoVO noticeDetailInfoVO=
                noticeMapper.selectDetail(id);
        log.debug("noticeDetailInfoVO = " + noticeDetailInfoVO);
        return JsonResult.ok(noticeDetailInfoVO);
    }
    @PostMapping("del")
    public JsonResult del(Integer id){
        log.debug("id = " + id);
        int i=noticeMapper.deleteById(id);
        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @PostMapping("update")
    public JsonResult update(@RequestBody NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam = "+noticeUpdateParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        int i=noticeMapper.updateNotice(notice);
        log.debug("i = " + i);
        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
